// Appendix2.8
log using Appendix2.8.log, replace
set maxvar 30000
use "C:\Users\sbstjp\OneDrive - Cardiff University\BES2024_W29_Panel_v29.1.dta" // Fieldhouse, E., J. Green, G. Evans, J. Mellon, C. Prosser, J. Bailey, R. de Geus, H. Schmitt, C. van der Eijk, J. Griffiths, & S. Perrett. (2024) British Election Study Internet Panel Waves 1-29. DOI: 10.5255/UKDA-SN-8202-2// Accessed on March 11 2025.

// Social justice scale
* Clean "don't know" responses 
foreach var in cwTransW26W27 cwAuthorsW26W27 cwLanguageW26W27 cwTrainingW26W27 {
    replace `var' = . if `var' == 9999
}

*Reverse variable so social justice coded high
foreach var in cwLanguageW26W27 cwTrainingW26W27 {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in cwTransW26W27 cwAuthorsW26W27 rcwLanguageW26W27 rcwTrainingW26W27 {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

*At this point, the scale has a Cronbach's Alpha of 0.73

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in scwTransW26W27 scwAuthorsW26W27 srcwLanguageW26W27 srcwTrainingW26W27 {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in scwTransW26W27 scwAuthorsW26W27 srcwLanguageW26W27 srcwTrainingW26W27 {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Demographics
* Delete missing values and rename
replace p_gross_householdW26 = . if inlist(p_gross_householdW26, 16, 17) 
rename gender FemaleGender
replace leftRightW26=. if leftRightW26==9999

*Generate dummies
gen SocCulEmployment=.
replace SocCulEmployment=0 if sectorW26W27W29==1
replace SocCulEmployment=1 if inrange(sectorW26W27W29, 2, 4)
replace SocCulEmployment=0 if inlist(sectorW26W27W29, 5, 8, 9) 

gen MinorityEthnic=. 
replace MinorityEthnic=0 if inlist(p_ethnicityW26, 1, 2)
replace MinorityEthnic=1 if inrange(p_ethnicityW26, 3, 15)

gen Graduate=.
replace Graduate=0 if inrange(p_edlevelUniW26, 0, 3)
replace Graduate=1 if inlist(p_edlevelUniW26, 4, 5)

gen Childless=.
replace Childless=1 if numChildrenW14==0
replace Childless=0 if inrange(numChildrenW14, 1, 6)

*Create interaction terms
gen FemGenChildlessInteraction = FemaleGender * Childless 
gen AgeFemGenInteraction = FemaleGender * ageW26

// Standardize 
rename Age age1
egen Age = std(ageW26)
egen Income = std(p_gross_householdW26)

// Regressions
regress SocJusValues Age FemaleGender Graduate Income MinorityEthnic SocCulEmployment [pweight=wt_new_W26], robust 
eststo
regress SocJusValues Age FemaleGender Graduate Income MinorityEthnic SocCulEmployment AgeFemGenInteraction [pweight=wt_new_W26], robust
eststo
regress SocJusValues Age FemaleGender Graduate Income MinorityEthnic SocCulEmployment Childless [pweight=wt_new_W26], robust 
eststo
regress SocJusValues Age FemaleGender Graduate Income MinorityEthnic SocCulEmployment Childless FemGenChildlessInteraction [pweight=wt_new_W26], robust 
eststo
regress SocJusValues Age FemaleGender Graduate Income MinorityEthnic SocCulEmployment [pweight=wt_new_W26] if leftRightW26<5, robust
eststo
esttab

log close

eststo clear

esttab using "Appendix2.8.rtf", ///
    b(3) se(3) ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    coeflabels(_cons "Constant") ///
    nonumbers replace

// Alternative specification
ologit SocJusValues Age FemaleGender Graduate Income MinorityEthnic SocCulEmployment [pweight=wt_new_W26], robust 
eststo

// Coefficient plot
regress SocJusValues Age FemaleGender Graduate Income MinorityEthnic [pweight=wt_new_W26], robust
label variable Age ""
label variable Income ""
label variable FemaleGender ""
coefplot, drop(_cons) xline(0, lcolor(red) lwidth(medium)) scheme(white_jet) xtitle("{bf: Social Justice Values (BES)}")

